﻿using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint.WebControls;
using System.Web.UI;

namespace IronSharePoint.Web.WebControls
{
    public class IronFieldControl : BaseFieldControl
    {
        public string ScriptPath { get; set; }

        protected IronScriptEngine engine;

        protected override void OnInit(EventArgs e)
        {
            engine = new IronScriptEngine();

            engine.CompileScriptFrom(ScriptPath);

            engine.Scope.SetVariable("this", this);

            if (engine.Scope.ContainsVariable("OnInit"))
            {
                object method = engine.Scope.GetVariable("OnInit");
                engine.Operations.Call(method, e);
            }

            base.OnInit(e);
        }

        protected override void OnLoad(EventArgs e)
        {
            if (engine.Scope.ContainsVariable("OnLoad"))
            {
                object method = engine.Scope.GetVariable("OnLoad");
                engine.Operations.Call(method, e);
            }

            base.OnLoad(e);
        }

        protected override string DefaultTemplateName
        {
            get
            {
                if (engine.Scope.ContainsVariable("DefaultTemplateName"))
                {
                    object method = engine.Scope.GetVariable("DefaultTemplateName");
                    return engine.Operations.Call(method) as String;
                }

                return base.DefaultTemplateName;
            }
        }


        protected override void CreateChildControls()
        {
            base.CreateChildControls();

            if (engine.Scope.ContainsVariable("CreateChildControls"))
            {
                object method = engine.Scope.GetVariable("CreateChildControls");
                engine.Operations.Call(method);
            }            
        }

        public override object Value
        {
            get
            {
                if (engine.Scope.ContainsVariable("GetValue"))
                {
                    object method = engine.Scope.GetVariable("GetValue");
                    return engine.Operations.Call(method);
                }
                else
                    return base.Value;
            }
            set
            {
                if (engine.Scope.ContainsVariable("SetValue"))
                {
                    object method = engine.Scope.GetVariable("SetValue");
                    engine.Operations.Call(method, value);
                }
                else
                    base.Value = value;
            }
        }

        public override void UpdateFieldValueInItem()
        {
            if (engine.Scope.ContainsVariable("UpdateFieldValueInItem"))
            {
                object method = engine.Scope.GetVariable("UpdateFieldValueInItem");
                engine.Operations.Call(method);
            }
            else
                base.UpdateFieldValueInItem();
        }

        protected override void RenderFieldForDisplay(System.Web.UI.HtmlTextWriter output)
        {
            if (engine.Scope.ContainsVariable("RenderFieldForDisplay"))
            {
                object method = engine.Scope.GetVariable("RenderFieldForDisplay");
                engine.Operations.Call(method, output);
            }
            else
                base.RenderFieldForDisplay(output);
        }

        protected override void RenderFieldForInput(System.Web.UI.HtmlTextWriter output)
        {
            if (engine.Scope.ContainsVariable("RenderFieldForInput"))
            {
                object method = engine.Scope.GetVariable("RenderFieldForInput");
                engine.Operations.Call(method, output);
            }
            else
                base.RenderFieldForInput(output);
        }
    }
}
